package com.axapoint.opoc.core.dao;

import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.Table;

import org.hibernate.annotations.Cascade;
import org.joda.time.DateTime;
import org.joda.time.Interval;

import com.anasoft.os.daofusion.bitemporal.WrappedBitemporalProperty;
import com.anasoft.os.daofusion.bitemporal.WrappedValueAccessor;

@Entity
@Table(name = "OrganizationChannel")
public class OrganizationChannel extends ObjectRegistration<OrganizationChannel, OrganizationChannel.Value, OrganizationChannel.Bitemporal> {
	private static final long serialVersionUID = 9168710745762860711L;

	@Entity
	@Table(name = "OrganizationChannelValues")
	public static final class Value extends _Value<OrganizationChannel, Bitemporal> {
		private static final long serialVersionUID = 1L;

		private String channel;
		
		@SuppressWarnings("deprecation")
		@ManyToOne(fetch = FetchType.LAZY)
	    @Cascade(value = { org.hibernate.annotations.CascadeType.ALL, org.hibernate.annotations.CascadeType.DELETE_ORPHAN })
	    @JoinColumn(name="organizationChannelTypeId")
		private OrganizationChannelType organizationChannelType;

		@SuppressWarnings("deprecation")
		@ManyToOne(fetch = FetchType.LAZY)
	    @Cascade(value = { org.hibernate.annotations.CascadeType.ALL, org.hibernate.annotations.CascadeType.DELETE_ORPHAN })
	    @JoinColumn(name="unitLocationId")
		private UnitLocation unitLocation;
		
		protected Value() {};
		Value(OrganizationChannel oc, String systemId, String userId, Operation operation, String comment, Value value) {
			super(oc, systemId, userId, operation, comment);
			this.organizationChannelType = value.organizationChannelType;
			this.unitLocation = value.unitLocation;
			this.channel = value.channel;
		}
		public Value(String channel, OrganizationChannelType organizationChannelType, UnitLocation unitLocation) {
			super();
			this.organizationChannelType = organizationChannelType;
			this.unitLocation = unitLocation;
			this.channel = channel;
		}
		@Override
		public String diff(_Value<OrganizationChannel, Bitemporal> otherValue, DateTime t0, DateTime t1) {
			StringBuffer sb = new StringBuffer();
			Value other = (Value)otherValue;
			
			if (channel != null && !channel.equals(other.channel)) {
				sb.append("OrganizationChannel.channel: ").append(channel).append(" - ").append(other.channel).append('\n');
			}

			OrganizationChannelType.Bitemporal l0 = getOrganizationChannelType().getValues().get(t0);
			OrganizationChannelType.Bitemporal l1 = getOrganizationChannelType().getValues().get(t1);

			if (!l0.getValue().getEntity().equals(l1.getValue().getEntity()) ||
				!l0.getValue().equals(l1.getValue())) {
				sb.append("OrganizationChannel.organizationChannelType: ");
				sb.append('[').append(l0.getValue().getEntity().getUuid()).append("] ").append(l0.getValue().getName()).append(" - ");
				sb.append('[').append(l0.getValue().getEntity().getUuid()).append("] ").append(l1.getValue().getName()).append('\n');
			}
			
			UnitLocation.Bitemporal u0 = getUnitLocation().getValues().get(t0);
			UnitLocation.Bitemporal u1 = getUnitLocation().getValues().get(t1);

			if (!u0.getValue().getEntity().equals(u1.getValue().getEntity()) ||
				!u0.getValue().equals(u1.getValue())) {
				sb.append("OrganizationChannel.unitLocation: ");
				sb.append('[').append(u0.getValue().getEntity().getUuid()).append("] ").append(u0.getValue().getOpeningHours()).append(" - ");
				sb.append('[').append(u0.getValue().getEntity().getUuid()).append("] ").append(u1.getValue().getOpeningHours()).append('\n');
			}

			return sb.toString();
		}
		@Override
		public String toString() {
			StringBuffer sb = new StringBuffer();
			sb.append(super.toString());
			sb.append("  channel=").append(channel).append('\n');
			sb.append("  channelType=").append(getOrganizationChannelType().getValues().get(getBitemporal().getValidityInterval().getStart()).getValue().getName()).append('\n');
			sb.append("  unitLocation=").append(getUnitLocation().getValues().get(getBitemporal().getValidityInterval().getStart()).getValue().getOpeningHours()).append('\n');

			return sb.toString();
		}
		public String getChannel() {
			return channel;
		}
		protected void setChannel(String channel) {
			this.channel = channel;
		}
		public OrganizationChannelType getOrganizationChannelType() {
			return organizationChannelType;
		}
		protected void setOrganizationChannelType(
				OrganizationChannelType organizationChannelType) {
			this.organizationChannelType = organizationChannelType;
		}
		public UnitLocation getUnitLocation() {
			return unitLocation;
		}
		protected void setUnitLocation(UnitLocation unitLocation) {
			this.unitLocation = unitLocation;
		}
	}
	
	@Entity
	@Table(name = "OrganizationChannelBitemporal")
	public static final class Bitemporal extends _Bitemporal<Bitemporal, Value> {
		private static final long serialVersionUID = -2132187534862370208L;
		
		protected Bitemporal() {}
	    private Bitemporal(OrganizationChannel.Value value, Interval validityInterval) {
	        super(value, validityInterval);
	    }
	    
	    @Override
	    public Bitemporal copyWith(Interval validityInterval) {
	    	return new Bitemporal(value, validityInterval);
	    }
	}

	public OrganizationChannel() {
		super();
	}
	
    public WrappedBitemporalProperty<Value, Bitemporal> getValues() {
    	return new WrappedBitemporalProperty<Value, Bitemporal>(values, new WrappedValueAccessor<Value,Bitemporal>() {
			private static final long serialVersionUID = 1L;

			@Override
			public Bitemporal wrapValue(Value value, Interval validityInterval) {
				Bitemporal bi = new Bitemporal(value, validityInterval);
				return bi;
			}
    	});
    }
}
